from django.db import models
from users.models import UserProfile


class Article(models.Model):
    title = models.CharField(verbose_name="标题",max_length=20,unique=True)
    content = models.TextField(verbose_name="内容")

    author = models.ForeignKey(UserProfile,verbose_name="作者",on_delete=models.CASCADE)

    publish_date = models.DateTimeField(auto_now=True,verbose_name="发布日期")

    def __repr__(self):
        return "{}-{}".format(self.author.username,self.title)

    __str__ = __repr__

    class Meta:
        verbose_name = "文章管理"
        verbose_name_plural = verbose_name


class Comment(models.Model):
    article = models.ForeignKey(Article,verbose_name="文章",related_name="comments",on_delete=models.CASCADE)

    user = models.ForeignKey(UserProfile,verbose_name="作者",on_delete=models.CASCADE)

    comment = models.TextField(max_length=1000,verbose_name="评论内容")

    date = models.DateTimeField(auto_now=True,verbose_name="日期")

    def __repr__(self):
        return "{}-{}".format(self.user.username,self.comment)

    __str__ = __repr__

    class Meta:
        verbose_name = "文章评论管理"
        verbose_name_plural = verbose_name


class CollectArticleModel(models.Model):
    user = models.ForeignKey(UserProfile,verbose_name="用户",on_delete=models.CASCADE)
    article = models.ForeignKey(Article,verbose_name="文章",on_delete=models.CASCADE,related_name="collect")

    def __repr__(self):
        return "{}-{}".format(self.user.username,self.article.title)

    __str__ = __repr__

    class Meta:
        verbose_name = "文章收藏管理"
        verbose_name_plural = verbose_name


